package com.itheima.upload4;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/*
上传图片的 客户端
1.读本地图片
2.向服务器上传图片
3.读取服务器的上传成功
 */
public class Client {

    public static void main(String[] args) throws IOException {
        Runnable runnable = new Runnable() {
            public void run() {
                Socket socket = null;
                BufferedInputStream bis = null;
                try {
                    socket = new Socket(InetAddress.getByName("127.0.0.1"), 8181);
                    OutputStream os = socket.getOutputStream();
                    //使用缓冲流  包装 普通流  提升 效率
                    BufferedOutputStream bos = new BufferedOutputStream(os);
                    //1.读本地图片
                    bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\美女\\meinv2.jpg"));
                    byte[] bytes = new byte[1024];
                    //2.向服务器上传图片
                    int len;
                    while ((len=bis.read(bytes))!=-1){
                        //bytes 0, len
                        bos.write(bytes,0,len);
                    }
                    bos.flush();
                    socket.shutdownOutput();


                    //3.读取服务器的上传成功
                    //字节
                    InputStream is = socket.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String s = br.readLine();
                    System.out.println(s);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    //释放资源
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }


            }
        };

        for (int i = 0; i < 100; i++) {
            new Thread(runnable).start();

        }

    }
}
